﻿#region References

using GalaSoft.MvvmLight.Command;
using Mss.Common.DomainObjects;
using Mss.Data;
using Mss.Managers;
using Mss.ViewModels.Base;

#endregion

namespace Mss.ViewModels
{
    /// <summary>
    /// View Model for the LogOn view.
    /// </summary>
    public class LogOnViewModel : ViewModelBase
    {
        #region Fields

        private User _currentUser;
        private bool _isLogOnFailed;
        private RelayCommand _logOnCommand;
        private Validation _validation;

        #endregion

        #region Constructor

        public LogOnViewModel()
        {
            CurrentUser = new User {UserName = "Test"};
            Validation = new Validation();
        }

        #endregion

        #region Properties

        public Validation Validation
        {
            get { return _validation; }
            private set
            {
                if (_validation == value) return;
                _validation = value;
                RaisePropertyChanged("Validation");
            }
        }

        /// <summary>
        /// Command which is trying to lo
        /// </summary>
        public RelayCommand LogOnCommand
        {
            get { return _logOnCommand ?? (_logOnCommand = new RelayCommand(logOn)); }
        }

        /// <summary>
        /// Current User.
        /// </summary>
        public User CurrentUser
        {
            get { return _currentUser; }
            private set
            {
                if (_currentUser == value) return;
                _currentUser = value;
                RaisePropertyChanged("CurrentUser");
            }
        }

        #endregion

        #region Commands Execute Callbacks

        private void logOn()
        {
            bool logOnResult = SecurityManager.Current.LogOn(CurrentUser.UserName, CurrentUser.Password);
            if (logOnResult == false)
            {
                Validation.Errors.Clear();
                Validation.Errors.Add(new Error("Logon Failed", "Please check your user name and password."));
                return;
            }

            Validation.Errors.Clear();
        }

        #endregion
    }
}